// subPages/pensionService/homeCare/homeCare.js
const app = getApp();
var util = require('../../../utils/util');
var num=4,p=1,loadMoreView,type
Page({
  /**
   * 页面的初始数据
   */
  data: {
      themeColor:app.globalData.themeColor,
      equipmentList:[
          {
              name:'睡眠仪',
              desc:'能够为您提供整夜的智能陪伴与健康监测服务为您的健康保驾护航',
              img:'/resources/homeCare/12.png',
              type:'2'
          }
      ],
      roleListInfo:[{
          name:'自己',
          user_id: app.globalData.userId
      }],
      roleList:['自己'],
      index:0,
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  //功能按钮
  functionBtn: function(e){
      var that = this
    if(that.data.userId){
        var type = e.currentTarget.dataset.type;
        let that = this;
        console.log(that.data.userId)
        switch (type) {
          case '1':
              wx.navigateTo({
                  url: '/subPages/equipment/watch/watch?user_id='+ that.data.userId,
              });
              break;
          case '2':
            wx.navigateTo({
              url: '/subPages/equipment/sleepIndx/sleepIndx?user_id='+ that.data.userId,
            })
            break;
        }
    }else{
        util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
          if (app.globalData.userInfo) {
            //已授权
            wx.redirectTo({
              url: '/pages/login/login'
            })
          }else{
            // 在没有 open-type=getUserInfo 版本的兼容处理
            if(app.globalData.isAuthorized){
                app.userInfoReadyCallback = res => {
                  if (res) {
                    //已授权
                    wx.redirectTo({
                      url: '/pages/login/login'
                    })
                  }else{
                    //未授权
                    wx.navigateTo({
                      url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                    })
                  }
                }
            }else{
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
            }
          }
        })
    }

  },
  bindPickerRole:function(e){
      let that = this;
      let index = e.detail.value;
      let roleListInfo = that.data.roleListInfo;
    //   console.log(roleListInfo[index].user_id);
      that.setData({
          index:index,
          userId: roleListInfo[index].user_id
      })
  },
  //获取关联关系
  getRole:function(){
      let that = this;
      if(that.data.userId){
        util.getAjax(`${app.globalData.apiUrl}/get_fid`,function(res){
            console.log(res);
            let roleListInfo = that.data.roleListInfo;
            let roleList = that.data.roleList;
            for(let i in res.data){
                roleListInfo.push(res.data[i]);
                roleList.push(res.data[i].name);
            }
            // console.log(roleListInfo);
            that.setData({
                roleListInfo:roleListInfo,
                roleList:roleList
            })
        },{
            user_id: that.data.userId
        })
      }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if(app.globalData.userId){
        that.setData({
          userId: app.globalData.userId
        })
        that.getRole();
    }else{
        app.userIdCallback = userId => {
            if (userId != '' && userId != null) {
                that.setData({
                    userId: userId
                });
                that.getRole();
            }
        }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
